//调用此接口 , 传入歌手 id, 可获得歌手部分信息和热门歌曲
export let artists = {
  namespaced: true,
  state: {
    artists_data: {},
    artists_hotSongs:{},
    available_data: [],
    available_flag: false
  },
  getters: {
    doneHotData(state) {
      if (state.artists_data.hotSongs) {
        if (state.artists_data.hotSongs.length > 5) {
          state.artists_hotSongs = state.artists_data.hotSongs.slice(0, 5);
        }
      }
      return state.artists_hotSongs;
    }
  },
  actions: {
    getArtists(store, data = {}) {
      let url = "/artists?id=" + data.id;
      return new Promise((resolve, reject) => {
        axios.get(url).then(({
          data
        }) => {
          let list = [];
          let promiseAll = data.hotSongs.map((item, index) => {
            return axios.get("/check/music?id=" + item.id).then(({
              data
            }) => {
              item.available = true;
              list.push(item.id);
            }).catch(error => {
              item.available = false;
            });
          });
          axios.all(promiseAll).then(resArr => {
            store.state.available_flag = true;
            store.state.available_data = list;
          }).catch(error => {});
          store.commit('setArtists', data);
          resolve(data);
        }).catch((error) => {
          if (error.response) {
            reject(error.response.data);
          }
        });
      });
    }
  },
  mutations: {
    setArtists(state, data) {
      state.artists_data = data;
    }
  }
}
